package com.example.musictutor.repository.facade.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.example.musictutor.conf.Constants;
import com.example.musictutor.domain.ProgressHistory;
import com.example.musictutor.factories.ProgressHistoryFactory;
import com.example.musictutor.repository.DBHelper;
import com.example.musictutor.repository.facade.DAOFacade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ironhulk on 2014/05/11.
 */
public class ProgressHistoryDAOFacade implements DAOFacade<ProgressHistory> {

    private SQLiteDatabase database;
    private DBHelper dbAdapter;
    private Context context;

    public ProgressHistoryDAOFacade(Context context) {
        this.context = context;
        dbAdapter = new DBHelper(context);
    }


    public void open()
    {
        database = dbAdapter.getWritableDatabase();
    }

    public void close()
    {
        dbAdapter.close();
    }


    @Override
    public ProgressHistory findEntityById(long id) {
        open();
        Cursor cursor = database.query(Constants.TABLE_USER_PROGRESS, new String[] { Constants.C_ID,
                        Constants.C_AMOUNT_OF_ATTEMPTS, Constants.C_AMOUNT_PASSED, Constants.C_AMOUNT_FAILED, Constants.C_LEVEL_ID, Constants.C_SONG_ID}, Constants.C_ID + "=?",
                        new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        HashMap<String,Integer> progressHistoryValues = new HashMap<String, Integer>();
        progressHistoryValues.put("amountOfAttempts",cursor.getInt(1));
        progressHistoryValues.put("amountPassed",cursor.getInt(2));
        progressHistoryValues.put("amountFailed",cursor.getInt(3));
        progressHistoryValues.put("level",cursor.getInt(4));
        progressHistoryValues.put("songId",cursor.getInt(5));
        ProgressHistory entity = ProgressHistoryFactory.createProgressHistory(id,progressHistoryValues);
        close();
        return entity;
    }

    public ProgressHistory getProgressHistoryForSongLevel(int songId){
        open();
        String songString = ""+songId;
        Cursor cursor = database.rawQuery("SELECT * FROM user_progress where song_id = ?", new String[]{songString});

        ProgressHistory progressHistory;

        if(cursor != null)
            cursor.moveToFirst();

        HashMap<String,Integer> values = new HashMap<String, Integer>();
        values.put("amountOfAttempts",cursor.getInt(1));
        values.put("amountPassed",cursor.getInt(2));
        values.put("amountFailed",cursor.getInt(3));
        values.put("level",cursor.getInt(4));
        values.put("songId",cursor.getInt(5));

        progressHistory = ProgressHistoryFactory.createProgressHistory(cursor.getInt(0),values);

        close();
        return progressHistory;

    }
    @Override
    public List<ProgressHistory> getEntities() {
        open();
        List<ProgressHistory> list = new ArrayList<ProgressHistory>();

        String selectQuery = "SELECT  * FROM " + Constants.TABLE_USER_PROGRESS;
        Cursor cursor = database.rawQuery(selectQuery, null);

        HashMap<String,Integer> progressHistoryValues;

        ProgressHistory entity;
        if (cursor.moveToFirst()) {
            do {
                progressHistoryValues = new HashMap<String, Integer>();
                progressHistoryValues.put("amountOfAttempts",cursor.getInt(1));
                progressHistoryValues.put("amountPassed",cursor.getInt(2));
                progressHistoryValues.put("amountFailed",cursor.getInt(3));
                progressHistoryValues.put("level",cursor.getInt(4));
                progressHistoryValues.put("songId",cursor.getInt(5));

                entity = ProgressHistoryFactory.createProgressHistory(cursor.getInt(0), progressHistoryValues);
                list.add(entity);
            } while (cursor.moveToNext());
        }

        close();
        return list;
    }

    @Override
    public void deleteEntity(long id) {
        open();
        database.delete(Constants.TABLE_USER_PROGRESS, Constants.C_ID + " = ?",
                new String[] { String.valueOf(id) });
        close();
    }

    @Override
    public void updateEntity(ProgressHistory entity) {
        open();

        ContentValues values = new ContentValues();
        values.put(Constants.C_AMOUNT_OF_ATTEMPTS, entity.getAmountOfAttempts());
        values.put(Constants.C_AMOUNT_PASSED, entity.getAmountPassed());
        values.put(Constants.C_AMOUNT_FAILED, entity.getAmountFailed());
        values.put(Constants.C_LEVEL_ID, entity.getLevel());
        values.put(Constants.C_SONG_ID, entity.getSongId());

        database.update(Constants.TABLE_USER_PROGRESS, values, Constants.C_ID + " = ?",
                new String[] { String.valueOf(entity.getId()) });
        close();
    }

    @Override
    public long createEntity(ProgressHistory entity) {
        open();

        ContentValues values = new ContentValues();
        values.put(Constants.C_AMOUNT_OF_ATTEMPTS, entity.getAmountOfAttempts());
        values.put(Constants.C_AMOUNT_PASSED, entity.getAmountPassed());
        values.put(Constants.C_AMOUNT_FAILED, entity.getAmountFailed());
        values.put(Constants.C_LEVEL_ID, entity.getLevel());
        values.put(Constants.C_SONG_ID, entity.getSongId());

        long id = database.insert(Constants.TABLE_USER_PROGRESS, null, values);

        close();
        return id;
    }
}
